#include "head_files.h"
#include "home_screen.h"

struct page home_page, p1, p2, p3, p4, p5;

void menu_home_page_init(void);
void menu_page1_init(void);
void menu_page2_init(void);
void menu_page3_init(void);
void menu_page4_init(void);
void menu_page5_init(void);

void menu_update(void)
{//used in StartDefaultTask loop
	
	static int pre_encoder_value = 0, pre_key_value = 0;
	encoder_read_value(&htim3,&encoder_s);
	if(pre_encoder_value != encoder_s.value[0] || pre_key_value != (encoder_s.key_enter & encoder_s.key_quit))
	{
		osDelay(100);
		
		encoder_state_recognize(&encoder_s);
		MenuEncoderCmd(encoder_s.state,encoder_s.speed[0]);
		MenuRender(1);
		
		pre_encoder_value = encoder_s.value[0];
		pre_key_value = (encoder_s.key_enter & encoder_s.key_quit);
		
		osDelay(100);
	}
}

void menu_init(void)
{//used in default_init func
	
  menu_home_page_init();
  menu_page1_init();
  menu_page2_init();
  menu_page3_init();
  menu_page4_init();
  menu_page5_init();
	
	//set as home screen
	MenuInit(&home_page);
}

void menu_home_page_init(void)
{
  add_subpage(&home_page, (char*)userTask1_attributes.name, &p1);
  add_subpage(&home_page, (char*)userTask2_attributes.name, &p2);
  add_subpage(&home_page, (char*)userTask3_attributes.name, &p3);
  add_subpage(&home_page, (char*)userTask4_attributes.name, &p4);
  add_subpage(&home_page, (char*)userTask5_attributes.name, &p5);
}

void menu_page1_init(void)
{
	add_func(&p1, (char*)userTask1_attributes.name, user_task1_init);
  add_func(&p1, (char*)userTask1_attributes.name, user_task1_deinit);
//	add_value(&p0, "[value]", (int *)&value, 1, NULL);
}

void menu_page2_init(void)
{
	add_func(&p2, (char*)userTask2_attributes.name, user_task2_init);
  add_func(&p2, (char*)userTask2_attributes.name, user_task2_deinit);
}

void menu_page3_init(void)
{
	add_func(&p3, (char*)userTask3_attributes.name, user_task3_init);
  add_func(&p3, (char*)userTask3_attributes.name, user_task3_deinit);
}

void menu_page4_init(void)
{
	add_func(&p4, (char*)userTask4_attributes.name, user_task4_init);
  add_func(&p4, (char*)userTask4_attributes.name, user_task4_deinit);
}

void menu_page5_init(void)
{
	add_func(&p5, (char*)userTask5_attributes.name, user_task5_init);
  add_func(&p5, (char*)userTask5_attributes.name, user_task5_deinit);
}
